
package com.hlkj.pay.app.merchant.impl;
/*
 * Hlpay-Plus aggregate payment system.
 * Copyright (c) 2024-2025 Hlpay Team Copyright has the right of final interpretation.
 */

import com.hlkj.pay.enums.MerchantEnum;
import com.hlkj.pay.infrastructure.model.merchant.MerchantNoticeDO;
import com.hlkj.pay.vo.merchant.resp.MerchantNoticeResp;
import org.springframework.stereotype.Service;

import com.hlkj.framework.common.pojo.CommonResult;
import com.hlkj.framework.common.util.json.JsonUtils;
import com.hlkj.pay.app.merchant.INoticeAppService;
import com.hlkj.pay.common.CommonResultCode;
import com.hlkj.pay.enums.CommonEnum;
import com.hlkj.pay.infrastructure.model.merchant.NoticeDO;
import com.hlkj.pay.service.common.IBusinessLogService;
import com.hlkj.pay.service.merchant.INoticeService;
import com.hlkj.pay.vo.common.req.CommonUpdateStatusReq;
import com.hlkj.pay.vo.merchant.req.NoticeReq;

import cn.hutool.core.bean.BeanUtil;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;

/**
 * @author HlpayTeam
 * @date 2024/11/06 16:33
 */
@Slf4j
@Service
@RequiredArgsConstructor
public class NoticeAppServiceImpl implements INoticeAppService {

    private final INoticeService noticeService;

    private final IBusinessLogService businessLogService;

    @Override
    public CommonResult<String> addNotice(NoticeReq noticeReq) {
        log.info("addNotice noticeReq:{}", JsonUtils.toJsonString(noticeReq));
        try {
            NoticeDO noticeDO = BeanUtil.copyProperties(noticeReq, NoticeDO.class);
            noticeService.addNotice(noticeDO);
            return CommonResult.success();
        }
        catch (Exception e) {
            log.error("addNotice error", e);
            return CommonResult.error(CommonResultCode.SERVICE_ERROR);
        }
    }

    @Override
    public CommonResult<Void> updateNotice(NoticeReq noticeReq) {
        log.info("updateNotice noticeReq:{}", JsonUtils.toJsonString(noticeReq));
        try {
            NoticeDO noticeDb = noticeService.queryById(noticeReq.getId());
            if (noticeDb == null) {
                return CommonResult.error(CommonResultCode.NOTICE_NOT_EXIST);
            }
            NoticeDO noticeDO = BeanUtil.copyProperties(noticeReq, NoticeDO.class);
            noticeDO.setId(noticeDb.getId());
            noticeService.updateNotice(noticeDO);
            businessChangeLog(noticeDb, noticeDO);
            return CommonResult.success();
        }
        catch (Exception e) {
            log.error("updateNotice error", e);
            return CommonResult.error(CommonResultCode.SERVICE_ERROR);
        }
    }

    @Override
    public CommonResult<Void> deleteNotice(Long id) {
        log.info("deleteNotice id:{}", id);
        try {
            if (id == null) {
                return CommonResult.error(CommonResultCode.PARAMETER_MISSING);
            }
            NoticeDO noticeDb = noticeService.queryById(id);
            if (noticeDb == null) {
                return CommonResult.error(CommonResultCode.NOTICE_NOT_EXIST);
            }
            noticeService.deleteNotice(id);
            NoticeDO noticeDO = BeanUtil.copyProperties(noticeDb, NoticeDO.class);
            noticeDO.setDeleteFlag(true);
            businessChangeLog(noticeDb, noticeDO);
            return CommonResult.success();
        }
        catch (Exception e) {
            log.error("deleteNotice error", e);
            return CommonResult.error(CommonResultCode.SERVICE_ERROR);
        }
    }

    @Override
    public CommonResult<Void> updateStatus(CommonUpdateStatusReq commonUpdateStatusReq) {
        log.info("updateStatus commonUpdateStatusReq:{}", JsonUtils.toJsonString(commonUpdateStatusReq));
        try {
            NoticeDO noticeDb = noticeService.queryById(commonUpdateStatusReq.getId());
            if (noticeDb == null) {
                return CommonResult.error(CommonResultCode.NOTICE_NOT_EXIST);
            }
            noticeService.updateStatus(commonUpdateStatusReq.getId(), commonUpdateStatusReq.getStatus());
            NoticeDO noticeDO = BeanUtil.copyProperties(noticeDb, NoticeDO.class);
            noticeDO.setStatus(commonUpdateStatusReq.getStatus());
            businessChangeLog(noticeDb, noticeDO);
            return CommonResult.success();
        }
        catch (Exception e) {
            log.error("updateStatus error", e);
            return CommonResult.error(CommonResultCode.SERVICE_ERROR);
        }
    }

    @Override
    public CommonResult<MerchantNoticeResp> merchantDetail(Long id) {
        log.info("merchantDetail id:{}", id);
        try {
            MerchantNoticeDO merchantNoticeDO = noticeService.queryMerchantNoticeById(id);
            if (merchantNoticeDO == null) {
                return CommonResult.error(CommonResultCode.NOTICE_NOT_EXIST);
            }
            NoticeDO noticeDO = noticeService.queryById(merchantNoticeDO.getNoticeId());
            if (CommonEnum.YES_NO_TYPE.NO.getCode().equals(merchantNoticeDO.getStatus())) {
                noticeService.updateMerchantNoticeStatus(id, CommonEnum.YES_NO_TYPE.YES.getCode());
            }
            return CommonResult.success(BeanUtil.copyProperties(noticeDO, MerchantNoticeResp.class));
        }
        catch (Exception e) {
            log.error("updateStatus error", e);
            return CommonResult.error(CommonResultCode.SERVICE_ERROR);
        }
    }

    /**
     * log object
     *
     * @param beforeNotice
     * @param afterNotice
     * @return
     */
    void businessChangeLog(NoticeDO beforeNotice, NoticeDO afterNotice) {
        businessLogService.addBusinessChangeLog(beforeNotice.getId().toString(), MerchantEnum.CHANGE_LOG_TYPE.NOTICE.getCode(), CommonEnum.CHANGE_LOG_MODEL_TYPE.COMMON,
                beforeNotice, afterNotice);
    }
}
